// pages/me/me.js
const API = require('../../utils/api.js');
const TOAST = require('../../utils/toast.js');
const AJAX = require('../../utils/ajax.js');
const FormatUtil = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    tempUserInfo: {},
    // 显示用户修改信息
    isShow: false,
    switch1:false,
    loading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if(options.form == 'authoriztion') {
      wx.setStorageSync('userId', options.userId)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUserInfo();
  },
  getUserInfo: function() {
    let that = this
    let userId = wx.getStorageSync('userId');
    let requestUrl = API.api.users + '/' + userId;
    this.setData({
      loading:true
    })
    AJAX.getRequest(requestUrl).then(response => {
      console.log(response.data)
      let userInfo = response.data;
      userInfo.monthBudget = FormatUtil.formatMoney(response.data.monthBudget / 100);
      let sendMessageResult = response.data.sendMessage == 1 ? true : false
      that.setData({
        userInfo: userInfo,
        isShow: false,
        switch1: sendMessageResult,
        loading:false
      })
      wx.setStorageSync('sendMessage', response.data.sendMessage)
    })
  },
  /**
   * 跳转到我的账单
   */
  gotoMyBill:function() {
    wx.navigateTo({
      url: '/pages/me/myBill/myBill?userid=' + wx.getStorageSync('userId')
    })
  },
  updateUserInfo: function (userId,userInfo) {
    let requestData = {}
    requestData.nickName = userInfo.nickName
    requestData.avatarUrl = userInfo.avatarUrl;
    let requestUrl = API.api.users + '/' + Number(userId);
    this.setData({
      loading: true
    })
    AJAX.putRequest(requestUrl, requestData, false).then(response => {
      console.log('用户信息更新成功啦')
      this.getUserInfo()
    }) 
  },
  // 判断用户是否进行授权操作
  bindUserInfo: function(eventhandle) {
    console.log('来到了bindgetUserInfo')
    let that = this
    if (eventhandle.detail.rawData) {
      // 获取用户信息成功
      console.log('获取用户信息成功')
      let userId = wx.getStorageSync('userId')
      wx.getUserInfo({
        success: res => {
          console.log(res)
          that.updateUserInfo(userId, res.userInfo)
        }
      })
     
    }
  },
  modalConfirm: function() {
    let that = this
    console.log(this.data.tempUserInfo.monthBudget)
    let tempUserInfo = this.data.tempUserInfo;
    let userId = wx.getStorageSync('userId');
    let requestUrl = API.api.users + '/' + userId;
    this.setData({
      loading: true
    })
    AJAX.putRequest(requestUrl, tempUserInfo).then(response => {
      console.log('更新成功！！！！');
      that.getUserInfo();
    })
  },
  modalCancel: function() {
    this.setData({
      isShow: false
    })
  },
  saveUsertell: function(e) {
    console.log('触发了!!!!' + e.detail.value)
    let userInfo = {};
    userInfo.monthBudget = e.detail.value * 100;
    this.setData({
      tempUserInfo: userInfo
    })
  },
  showModel: function() {
    console.log('显示月预算')
    this.setData({
      isShow: true
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 切换是否发送模板消息
   */
  onChange(event) {
    const detail = event.detail;
    let requestData = {}
    requestData.sendMessage = detail.value ? 1 : 0
    let userId = wx.getStorageSync('userId');
    let requestUrl = API.api.users + '/' + Number(userId);
    this.setData({
      loading: true
    })
    AJAX.putRequest(requestUrl, requestData, false).then(response => {
      console.log('用户信息更新成功啦')
      this.getUserInfo()
    }) 
    // console.log(detail)
    // this.setData({
    //   'switch1': detail.value
    // })

  }
})